package com.xiaoxie.controller;


import com.xiaoxie.DTO.*;
import com.xiaoxie.entity.BaseContext;
import com.xiaoxie.entity.Result;
import com.xiaoxie.entity.User;
import com.xiaoxie.entity.Word;
import com.xiaoxie.mapper.UserDao;
import com.xiaoxie.service.IWord_tbService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 小谢
 * @since 2023-10-13
 */
@RestController
@RequestMapping("/word")
@CrossOrigin
public class Word_tbController {
    private static final Logger log = LoggerFactory.getLogger(Word_tbController.class);
    @Autowired
    private IWord_tbService word_tbService;

    @Autowired
    private UserDao userDao;


    /**
     * 获取当前需要记忆的单词
     *
     * @return
     */
    @PostMapping
    public Result<List<Word>> selectNeedMemory() {

        List<Word> words = word_tbService.getNeedMemory(BaseContext.getCurrentAccount());
        return Result.success(words);
    }

    /**
     * 更新单词数据
     *
     * @param wordDTO
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody wordUpdateDTO wordDTO) {
        boolean update = word_tbService.update(wordDTO,BaseContext.getCurrentAccount());
        return update ? Result.success() : Result.error("更新失败");
    }

    /**
     * 添加单词
     * @param wordDTO
     * @return
     */
    @PostMapping("/insert")
    public Result<String> Insert(@RequestBody wordUpdateDTO wordDTO){
        log.info("添加单词{}", wordDTO.getWord());
        Boolean insert = word_tbService.insert(wordDTO,BaseContext.getCurrentAccount());
        return  insert ? new Result<>(200, "添加成功", null) : Result.error("单词已存在");
    }

    /**
     * 获取分页数据
     * @param pageDTO
     * @return
     */
    @PutMapping("/pageList")
    public Result<List<Word>> pageList(@RequestBody PageDTO pageDTO){
//        log.info("分页查询{}", pageDTO);
        List<Word> list= word_tbService.pageList(pageDTO,BaseContext.getCurrentAccount());
//        log.info("分页查询{}单词{}", pageDTO,list);
        return Result.success(list);
    }

    /**
     * 获取所有单词，目前用于前端判断分页
     * @return
     */
    @GetMapping
    public Result<List<Word>> getWordList(){
        List<Word> list=word_tbService.getWordList(BaseContext.getCurrentAccount());
        return Result.success(list);
    }

    /**
     * 单词数据修改
     * @param modifyWordDTO
     * @return
     */
    @PutMapping("/modify")
    public Result<String> modifyWord(@RequestBody modifyWordDTO modifyWordDTO){
        word_tbService.wordModify(modifyWordDTO);
        log.info("修改单词{}",modifyWordDTO);
        return Result.success();
    }

    /**
     * 删除单词
     * @param wordId
     * @return
     */
    @DeleteMapping("/{wordId}")
    public Result<String> deleteWord(@PathVariable Integer wordId){
        word_tbService.deleteWord(wordId);
        return Result.success();
    }

    /**
     * 快刷模块获取数据
     * @return
     */
    @PostMapping("/quickChoose")
    public Result<List<quick_chooseDTO>> quickChoose(){
        List<quick_chooseDTO>    quickChooseDTOS =
                word_tbService.quick_choose(BaseContext.getCurrentAccount());
        User user = userDao.select(BaseContext.getCurrentAccount());
        if (user.getLearnFinish().equals("false"))
            return new Result<>(2001, "学习规划单词", quickChooseDTOS);
        else
             return Result.success(quickChooseDTOS);
    }

    /**
     * 速刷模块更新单词数据

     */
    @PutMapping("/quickUpdate")
    public Result<String> quickUpdate(@RequestBody quickupdateDTO quickupdateDTO){
        word_tbService.quickUpdate(quickupdateDTO.getId(),quickupdateDTO.getAction(),BaseContext.getCurrentAccount());
        return Result.success();
    }
}

